package cn.shyouchi.wuguanjia.android.ui.home.security;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.event.LogoutEvent;
import cn.shyouchi.wuguanjia.android.event.ModuleChangeEvent;
import cn.shyouchi.wuguanjia.android.event.NoticeStateChange;
import cn.shyouchi.wuguanjia.android.ui.QRCode.QRCodeActivity;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.application.ApplicationFragment;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.contact.ContactFragment;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.main.MainFragment;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.me.MeFragment;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.MessageCenterFragment;
import cn.shyouchi.wuguanjia.android.ui.search.SearchActivity;
import cn.shyouchi.wuguanjia.android.widget.MainNavigateTabBar;

public class SecurityMainActivity extends BaseActivity implements
        MainNavigateTabBar.OnTabSelectedListener {
    private static final String TAG_PAGE_HOME = "首页";
    private static final String TAG_PAGE_CITY = "通讯录";
    private static final String TAG_PAGE_PUBLISH = "消息";
    private static final String TAG_PAGE_MESSAGE = "应用";
    private static final String TAG_PAGE_PERSON = "我的";


    @BindView(R.id.main_container)
    FrameLayout mainContainer;
    @BindView(R.id.mainTabBar)
    MainNavigateTabBar mNavigateTabBar;
    @BindView(R.id.tab_post_icon)
    ImageView tabPostIcon;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    boolean mIsExit;

    Map<String, Fragment> mFragmentMap = new HashMap<>();

    UserStorage mUserStorage;

    @Override
    public int initContentView() {
        return R.layout.activity_security_main;
    }

    @Override
    public void initInjector() {
        mUserStorage = new UserStorage(mContext);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);

        EventBus.getDefault().register(this);

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();

        mNavigateTabBar.addTab(new MainNavigateTabBar.TabParam(R.mipmap.ic_home_bottom_tab_main, R.mipmap.ic_home_bottom_tab_main_selected, TAG_PAGE_HOME));
        mNavigateTabBar.addTab(new MainNavigateTabBar.TabParam(R.mipmap.ic_home_bottom_tab_contact, R.mipmap.ic_home_bottom_tab_contact_selected, TAG_PAGE_CITY));
        mNavigateTabBar.addTab(new MainNavigateTabBar.TabParam(0, 0, TAG_PAGE_PUBLISH));
        mNavigateTabBar.addTab(new MainNavigateTabBar.TabParam(R.mipmap.ic_home_bottom_tab_app, R.mipmap.ic_home_bottom_tab_app_selected, TAG_PAGE_MESSAGE));
        mNavigateTabBar.addTab(new MainNavigateTabBar.TabParam(R.mipmap.ic_home_bottom_tab_me, R.mipmap.ic_home_bottom_tab_me_selected, TAG_PAGE_PERSON));
        mNavigateTabBar.setTabSelectListener(this);

        mNavigateTabBar.setCurrentSelectedTab(0);

        mFragmentTransaction.add(R.id.main_container, getFragmentByKey(TAG_PAGE_HOME));
        mFragmentTransaction.commit();


    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    public void onTabSelected(MainNavigateTabBar.ViewHolder holder) {
//        ToastUtil.showToast(holder.tabTitle.getText().toString());
//        if (holder.tabTitle.getText().equals("我的")) {
//            startActivity(new Intent(mContext, ProfileActivity.class));
//        } else {
        toggleFragment(holder.tabTitle.getText().toString());
//        }

    }


    public void onMainTabClick(View v) {


        toggleFragment(TAG_PAGE_PUBLISH);

//        startActivity(new Intent(mContext, MessageActivity.class));
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            startActivity(new Intent(mContext, QRCodeActivity.class));
            return true;
        }
        switch (item.getItemId()) {
            case R.id.search:
                startActivity(new Intent(mContext, SearchActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public void onBackPressed() {
        if (mIsExit) {
            this.finish();
        } else {
            Toast.makeText(this, "再按一次退出物管家", Toast.LENGTH_SHORT).show();
            mIsExit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mIsExit = false;
                }
            }, 2000);
        }
    }


    private void toggleFragment(String key) {
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.main_container, getFragmentByKey(key));
        mFragmentTransaction.commit();
    }

    private Fragment getFragmentByKey(String key) {
        if (mFragmentMap.containsKey(key)) {
            return mFragmentMap.get(key);
        } else {
            Fragment f = null;
            switch (key) {
                case TAG_PAGE_HOME:
                    f = new MainFragment();
                    break;
                case TAG_PAGE_CITY:
                    f = new ContactFragment();
                    break;
                case TAG_PAGE_MESSAGE:
                    f = new ApplicationFragment();
                    break;
                case TAG_PAGE_PERSON:
                    f = new MeFragment();
                    break;
                case TAG_PAGE_PUBLISH:
                    f = new MessageCenterFragment();
                    break;
            }
            mFragmentMap.put(key, f);
            return f;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLogoutEvent(LogoutEvent event) {
        mUserStorage.clear();
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainModuleChange(ModuleChangeEvent event) {
        Fragment f = mFragmentMap.get(TAG_PAGE_HOME);
        if (f != null) {
            ((MainFragment) f).needRefresh();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNoticeStateChange(NoticeStateChange event) {
        Fragment f = mFragmentMap.get(TAG_PAGE_PUBLISH);
        if (f != null) {
            ((MessageCenterFragment) f).refreshNoticeState();
        }
    }


}
